package com.wuwei.business.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.wuwei.business.Constant.CarConstant;
import com.wuwei.business.domain.BusCar;
import com.wuwei.business.domain.BusRent;
import com.wuwei.business.mapper.BusRentMapper;
import com.wuwei.common.constant.Constants;
import com.wuwei.common.core.domain.entity.SysDept;
import com.wuwei.common.core.domain.model.LoginUser;
import com.wuwei.common.utils.DateUtils;
import com.wuwei.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wuwei.business.mapper.BusCustomerMapper;
import com.wuwei.business.domain.BusCustomer;
import com.wuwei.business.service.IBusCustomerService;
import org.springframework.util.ObjectUtils;

/**
 * 客户增删改查Service业务层处理
 * 
 * @author wuwei
 * @date 2021-12-28
 */
@Service
@Slf4j
public class BusCustomerServiceImpl implements IBusCustomerService 
{
    @Autowired
    private BusCustomerMapper busCustomerMapper;
    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    private BusRentMapper busRentMapper;

    /**
     * 查询客户增删改查
     * 
     * @param id 客户增删改查主键
     * @return 客户增删改查
     */
    @Override
    public BusCustomer selectBusCustomerById(Long id)
    {
        return busCustomerMapper.selectBusCustomerById(id);
    }

    /**
     * 查询客户增删改查列表
     * 
     * @param busCustomer 客户增删改查
     * @return 客户增删改查
     */
    @Override
    public List<BusCustomer> selectBusCustomerList(BusCustomer busCustomer)
    {
        return busCustomerMapper.selectBusCustomerList(busCustomer);
    }

    /**
     * 新增客户增删改查
     * 
     * @param busCustomer 客户增删改查
     * @return 结果
     */
    @Override
    public int insertBusCustomer(BusCustomer busCustomer)
    {
        busCustomer.setCreateTime(DateUtils.getNowDate());
        return busCustomerMapper.insertBusCustomer(busCustomer);
    }

    /**
     * 修改客户增删改查
     * 
     * @param busCustomer 客户增删改查
     * @return 结果
     */
    @Override
    public int updateBusCustomer(BusCustomer busCustomer)
    {
        busCustomer.setUpdateTime(DateUtils.getNowDate());
        return busCustomerMapper.updateBusCustomer(busCustomer);
    }

    /**
     * 批量删除客户增删改查
     * 
     * @param ids 需要删除的客户增删改查主键
     * @return 结果
     */
    @Override
    public int deleteBusCustomerByIds(Long[] ids)
    {
        return busCustomerMapper.deleteBusCustomerByIds(ids);
    }

    /**
     * 删除客户增删改查信息
     * 
     * @param id 客户增删改查主键
     * @return 结果
     */
    @Override
    public Integer deleteBusCustomerById(Long id)
    {
        return busCustomerMapper.deleteBusCustomerById(id);
    }

    @Override
    public List<BusCustomer> selectCustomerPage(LoginUser loginUser, BusCustomer busCustomer) {
        return busCustomerMapper.selectCustomerPage(loginUser,busCustomer);
    }

    /**
     *插入用户
     * @param busCustomer
     * @param loginUser
     * @return
     */

    @Override
    public int insertCustomer(BusCustomer busCustomer, LoginUser loginUser) {
        log.info("新增用户{}，当前用户id为{}", JSON.toJSONString(busCustomer),loginUser.getUserId());
        busCustomer.setCreateTime(new Date());
        busCustomer.setUpdateTime(new Date());
        busCustomer.setCreateBy(loginUser.getUsername());
        busCustomer.setDelFlag(CarConstant.DEL_FLAG_UNDEL);
        // 处理部门的名称
        // 拿到部门的id  查询部门的名称
        SysDept sysDept = iSysDeptService.selectDeptById(busCustomer.getDeptId());
        busCustomer.setDeptName(sysDept.getDeptName());
        return busCustomerMapper.insertBusCustomer(busCustomer);
    }

    /**
     * 修改用户
     * @param loginUser
     * @param busCustomer
     * @return
     */
    @Override
    public int updateCustomer(LoginUser loginUser, BusCustomer busCustomer) {
        log.info("修改用户{}，当前用户id为{}", JSON.toJSONString(busCustomer),loginUser.getUserId());
        busCustomer.setUpdateTime(new Date());
        busCustomer.setUpdateBy(loginUser.getUsername());
        // 处理部门的名称
        // 拿到部门的id  查询部门的名称
        SysDept sysDept = iSysDeptService.selectDeptById(busCustomer.getDeptId());
        busCustomer.setDeptName(sysDept.getDeptName());
        return busCustomerMapper.updateBusCustomer(busCustomer);
    }

    /**
     * 假删除 修改del_flag
     * @param loginUser
     * @param ids
     * @return
     */
    @Override
    public int delCustomer(LoginUser loginUser, List<Long> ids) {
        log.info("删除用户{}，当前用户id为{}", JSON.toJSONString(ids),loginUser.getUserId());
        return busCustomerMapper.deleteDCustomerIds(ids,loginUser);
    }

    /**
     * 通过身份证查表是否登记
     * @param identity
     * @return
     */
    @Override
    public Integer checkCustomerId(String identity) {
        BusCustomer busCustomer=busCustomerMapper.selectByIdentity(identity);
        if (ObjectUtils.isEmpty(busCustomer)) {
            throw  new IllegalArgumentException("客户身份证号不存在请登记后再来");
        }
        BusRent busRent =busRentMapper.selectRentByIdentity(identity, CarConstant.CAR_NOT_REBACK);
        if (!ObjectUtils.isEmpty(busRent)){
            throw  new IllegalArgumentException("该用户有车辆未归还");
        }
        return 1;


    }
}
